package com.blockmeta.bbs.overallserviceapplication.activity;

import android.graphics.Bitmap;

import androidx.viewpager.widget.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.blockmeta.bbs.baselibrary.utils.permission.RequestPermissionUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.umeng.ScreenShotUtil;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.R2;
import com.blockmeta.bbs.overallserviceapplication.widget.PicViewPager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by author on 2018/3/20.
 */
//@Route(path = RouterPath.VIEWPAGER_ACTIVITY)
public class ViewPagerActivity extends BaseActivity {
    @BindView(R2.id.view_pager)
    PicViewPager mViewPager;
    @Autowired(name = Constant.ZERO)
    public ArrayList<String> mPics;
    @Autowired(name = Constant.ONE)
    public int currentPosition = 0;


    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        setTitleText("浏览图片");
        return LayoutInflater.from(mContext).inflate(R.layout.activity_view_pager, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        //如果通过intent传递的图片地址为空，表示是通过静态变量传值，因为intent传值有限
        if(mPics == null || mPics.size() == 0){
            mPics = Constant.TempImgUrls;
            Constant.TempImgUrls = null;
        }
        mViewPager.setAdapter(new SamplePagerAdapter());
        mViewPager.setCurrentItem(currentPosition, false);
    }

    class SamplePagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mPics==null?0:mPics.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, final int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            if (mPics == null || mPics.size() == 0) {
                return photoView;
            }

            if (!ViewPagerActivity.this.isFinishing()) {
                if (TextUtils.isEmpty(mPics.get(position)))return photoView;
                Picasso.with(mContext).load(mPics.get(position)).into(photoView);
                photoView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                        RequestPermissionUtil.requestStorage(new RequestPermissionUtil.OnPermissionGrantedListener() {
                                                            @Override
                                                            public void onPermissionGranted() {

//                                                                Glide.with(mContext).load(mPics.get(position)).asBitmap().into(new SimpleTarget<Bitmap>() {
//                                                                    @Override
//                                                                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
//                                                                        ScreenShotUtil.savePicture(mContext, bitmap, 100, true);
//                                                                    }
//                                                                });
                                                            }

                                                        }
                        );
                        return true;
                    }


                });
            }
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}
